#ifndef _THREADPOOLWORKITEM_H_
#define _THREADPOOLWORKITEM_H_

#include <stdint.h>

#include "common/sys/ThreadPoolDispatcher.h"
#include "common/log/log.h"

/**
 * @brief 线程池任务类接口
 */
class ThreadPoolWorkItem
{
public:
    ThreadPoolWorkItem():
        failed_(false),
        dispatcher_(NULL)
    {}
    virtual ~ThreadPoolWorkItem() {}

    virtual void init(ThreadPoolDispatcher *p_dispatcher)
    {
        dispatcher_ = p_dispatcher;
    }
    /**
     * @brief 在执行具体业务前的公共操作
     *
     * @return
     */
    virtual int preProcess()
    {
        return SUCCESSFUL;
    }

    /**
     * @brief 实现此方法以执行具体业务
     *
     * @return
     */
    virtual int process() = 0;

    /**
     * @brief 执行具体业务后的公共操作
     *
     * @return
     */
    virtual int postProcess()
    {
        if(dispatcher_)
            dispatcher_->postResult( this );
        else
        {
            ERROR_LOG("ThreadPoolWorkItem::postProcess() dispatcher null");
            return FAILED;
        }
        
        return SUCCESSFUL;
    }

    virtual uint64_t getTaskID()
    {
        return taskID_;
    }

    virtual void setTaskID( uint64_t id )
    {
        taskID_ = id;
        return;
    }

    void set_failed() 
    {
        failed_ = true;
    }

    bool get_failed() const
    {
        return failed_;
    }

private:
    uint64_t taskID_;
    bool failed_;

    ThreadPoolDispatcher *dispatcher_;
};

#endif // _THREADPOOLWORKITEM_H_

